package com.lyxs916.testlibrary.viewpagerframent;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.lyxs916.adapter.recyclerview.CommonAdapter;
import com.lyxs916.adapter.recyclerview.click.OnItemClickListener;
import com.lyxs916.appbaselibrary.core.fragment.BaseListFragment;
import com.lyxs916.testlibrary.R;
import com.lyxs916.utilslibrary.ui.ToastUtils;
import com.lyxs916.utilslibrary.ui.divider.HorizontalDividerItemDecoration;
import com.lyxs916.xrecycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * 说明:陪护-我的订单
 * 360621904@qq.com 杨阳 2017/3/24  14:16
 */
@SuppressLint("ValidFragment")
public class OrderFragment extends BaseListFragment<OrderParentInterfaces.IOrder, OrderPresenter> implements OrderParentInterfaces.IOrder {
    /**
     * 0:待支付; 1:待服务; 2:服务中;4:已完成; 7:待接单
     */
    private int mType;
    ArrayList<NurserOrderdData.ObjsBean> mDatas = new ArrayList<>();
    NurserOrderAdapter adapter;


    @SuppressLint("ValidFragment")
    public OrderFragment() {
    }

    public OrderFragment(int type) {
        this.mType = type;
    }

    public static OrderFragment getInstance(int type) {
        return new OrderFragment(type);
    }

    @Override
    public int getContentView() {
        return R.layout.test_activity_list;
    }

    @Override
    public void initView2() {

        presenter.getHttpData(0, true, mType);

        adapter.setOnItemClickListener(new OnItemClickListener<NurserOrderdData.ObjsBean>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, NurserOrderdData.ObjsBean data, int position) {
                ToastUtils.getToastShort("AAAAAAAAAAAAA");


                Intent intent = new Intent();
                intent.setClassName(mActivity, "com.business.yang.mvp.view.my.activity.MyActivity");
                mActivity.startActivity(intent);

                presenter.getHttpData(0, true, mType);
                mActivity.finish();

            }
        });
    }

    @Override
    public CommonAdapter getAdapter() {

        return adapter = new NurserOrderAdapter(this, mDatas);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            //详情页面操作后列表页要刷新
            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
                if (data.getBooleanExtra("isRefresh", false)) {
                    presenter.getHttpData(0, true, mType);
                }
            }
        }
        //评价成功
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK + 1) {
            presenter.getHttpData(0, true, mType);
        }
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return new HorizontalDividerItemDecoration.Builder(mActivity).colorResId(R.color.gray_ee).sizeResId(R.dimen.dp_10).build();
    }

    @Override
    public void onRefresh() {
        presenter.getHttpData(0, true, mType);
    }


    @Override
    public void getHttpData(int total, List<NurserOrderdData.ObjsBean> datas) {
        mDatas.addAll(datas);
        adapter.notifyDataSetChanged();
        int postiont = 0;

        // 0:待支付; 1:待服务; 2:服务中;4:已完成; 7:待接单
        FragmentManagerActivity ctivity = (FragmentManagerActivity) mActivity;

        ctivity.setCount(ctivity.viewPage.getCurrentItem(), mDatas.size());
    }


    /**
     * 显示不同的订单状态
     */
    public void setOrderStatus(int type) {
        presenter.getHttpData(type, true, mType);
    }

    @Override
    public void onLoadding() {

    }

    @Override
    public void clearData() {

    }

    @Override
    public void coreOnLoadingBarListener(View parent) {

    }

    @Override
    public SuperRecyclerView getSuperRecyclerView() {
        return (SuperRecyclerView) view.findViewById(R.id.superRecyclerView);
    }

    @Override
    public OrderPresenter initPresenter() {
        return new OrderPresenter();
    }
}
